package controller.admins;

import com.alibaba.fastjson.JSON;
import dao.admins.ISellerQuery;
import dao.admins.impl.SellerQueryImpl;
import pojo.Seller;
import util.pagingOpt.impl.PagingImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.List;

@WebServlet(name = "DoQueryServlet", urlPatterns = "/doSel")
public class DoQueryServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        ISellerQuery sellerQuery = new SellerQueryImpl();
        String action = request.getParameter("action");
        if ("selQuery".equals(action)){
            selQuery(request);
            request.getRequestDispatcher("/page/seller/sellerQuery.jsp").forward(request, response);
        }else if ("selQueryById".equals(action)){
            selQueryById(request, sellerQuery);
            request.getRequestDispatcher("/page/seller/sellerUpdate.jsp").forward(request, response);
        }else if ("selUpdate".equals(action)){
            selUpdate(request, sellerQuery);
            selQuery(request);
            request.getRequestDispatcher("/page/seller/sellerQuery.jsp").forward(request, response);
        }else if ("selAdd".equals(action)){
            selAdd(request, sellerQuery);
            selQuery(request);
            request.getRequestDispatcher("/page/seller/sellerQuery.jsp").forward(request, response);
        }else if ("selDelById".equals(action)){
            selDelById(request, sellerQuery);
            selQuery(request);
            request.getRequestDispatcher("/page/seller/sellerQuery.jsp").forward(request, response);
        }else if ("paging".equals(action)){
            PrintWriter out = response.getWriter();
            int pageNo = Integer.parseInt(request.getParameter("pageNo"));
//            JSONObject json = new JSONObject();
//            for (Seller s : paging(pageNo).getList()){
//                System.out.println("seller" +  s.getId());
//            }
//            System.out.println("pageSize: " + paging(pageNo).getPageEntity().getPageSize());
//            System.out.println("totalCount: " + paging(pageNo).getPageEntity().getTotalCount());
//            System.out.println("currentPage:" + paging(pageNo).getPageEntity().getCurrentPage());
//            System.out.println("totalPage:" + paging(pageNo).getPageEntity().getTotalPage());
//            System.out.println(paging(pageNo).getPageEntity().getLists());
//            json.put("seller", paging(pageNo).getPageEntity());
            String pageSupportJson =JSON.toJSONStringWithDateFormat(paging(pageNo).getPageEntity(),"yyyy-MM-dd");
            out.write(pageSupportJson);
//            out.print(json);
        }
    }

    private void selQuery(HttpServletRequest request){
        List<Seller> sellers = paging(1).getList();
        request.setAttribute("pageData", sellers);
        request.setAttribute("pageNumber", request.getParameter("pageNumber"));
    }

    private void selQueryById(HttpServletRequest request, ISellerQuery sellerQuery){
        long id = Long.valueOf(request.getParameter("id"));
        request.setAttribute("Sel", sellerQuery.querySellerById(id));
    }

    private void selUpdate(HttpServletRequest request, ISellerQuery sellerQuery){
        Seller seller = new Seller();
        String date = request.getParameter("sellerBirthday") + " 00:00:00";
        SellerQueryImpl.setSeller(seller, Integer.parseInt(request.getParameter("id")), request.getParameter("sellerName"), request.getParameter("sellerUser"),
                request.getParameter("sellerPassword"), request.getParameter("sellerSex"), Timestamp.valueOf(date), request.getParameter("sellerIdCard"),
                request.getParameter("sellerEmail"), request.getParameter("sellerTel"), request.getParameter("sellerAddress"));
        sellerQuery.updateSeller(seller);
    }

    private void selAdd(HttpServletRequest request, ISellerQuery sellerQuery){
        Seller seller = new Seller();
        String date = request.getParameter("sellerBirthday") + " 00:00:00";
        SellerQueryImpl.setSeller(seller, 0, request.getParameter("sellerName"), request.getParameter("sellerUser"),
                request.getParameter("sellerPassword"), "男", Timestamp.valueOf(date), request.getParameter("sellerIdCard"),
                request.getParameter("sellerEmail"), request.getParameter("sellerTel"), request.getParameter("sellerAddress"));
        sellerQuery.addSeller(seller);
    }

    private void selDelById(HttpServletRequest request, ISellerQuery sellerQuery){
        long id = Integer.parseInt(request.getParameter("id"));
        sellerQuery.deleteSeller(id);
    }

    private PagingImpl paging(int pageNo){
        String sqlQuery = "select count(*) from seller";
        String sqlPaging = "select * from seller limit ?, ?";
        PagingImpl paging = new PagingImpl(3, sqlQuery, sqlPaging);
        paging.setCurrentPage(pageNo);
        return paging;
    }
}
